package com.vainycos.lotterysearch.service.notify;

import com.vainycos.lotterysearch.model.BigLotteryBall;
import com.vainycos.lotterysearch.model.DoubleColorBall;
import com.vainycos.lotterysearch.model.LuckyLottery;
import com.vainycos.lotterysearch.service.BigLotteryService;
import com.vainycos.lotterysearch.service.DoubleColorService;
import com.vainycos.lotterysearch.service.LuckyLotteryService;
import com.vainycos.lotterysearch.utils.DingTalkPushUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @author: Vainycos
 * @description 钉钉群机器人通知
 * @date: 2022/11/14 14:35
 */
@Slf4j
@Component
public class DingNotify {

    @Autowired
    private BigLotteryService bigLotteryService;

    @Autowired
    private DoubleColorService doubleColorService;

    @Autowired
    private DingTalkPushUtil dingTalkPushUtil;

    @Autowired
    private LuckyLotteryService luckyLotteryService;

    @Value("${ding.msg-prefix:}")
    private String dingMsgPrefix;

    /**
     * 推送普通文本消息
     * @param content
     */
    public void notifyText(String content){
        dingTalkPushUtil.pushText(content);
    }

    /**
     * 推送md消息
     * @param content
     */
    public void notifyMarkdown(String title, String content){
        dingTalkPushUtil.pushMarkdown(title, content);
    }

    /**
     * 大乐透中奖通知
     *
     * @param updateBigLotteryBall
     */
    public void notifyBigLotteryBall(List<BigLotteryBall> updateBigLotteryBall) {
        updateBigLotteryBall.forEach(e -> {
            String subject = e.getLotteryDate() + "-" + e.getPeriodId() + "期" + "超级大乐透开奖";
            log.info("开始推送钉钉群->{}", subject);
            // 发送普通消息
            // dingTalkPushUtil.pushText(dingMsgPrefix + subject + "。" + bigLotteryService.describe(e));
            dingTalkPushUtil.pushMarkdown(subject, bigLotteryService.describeForMarkdown(subject, e));
        });
    }

    /**
     * 双色球中奖通知
     *
     * @param updateDoubleColorBall
     */
    public void notifyDoubleColorBall(List<DoubleColorBall> updateDoubleColorBall) {
        updateDoubleColorBall.forEach(e -> {
            String subject = e.getLotteryDate() + "-" + e.getPeriodId() + "期" + "双色球开奖";
            log.info("开始推送钉钉群->{}", subject);
            dingTalkPushUtil.pushMarkdown(subject, doubleColorService.describeForMarkdown(subject, e));
        });
    }

    /**
     * 支持多种类型提醒
     * @param luckyLottery
     */
    public void notifyLuckyLottery(LuckyLottery luckyLottery){
        String subject = luckyLottery.getExpect() + "-" + luckyLottery.getName();
        String description = luckyLotteryService.describeForMarkdown(luckyLottery);
        log.info("开始推送钉钉群->{}", subject);
        dingTalkPushUtil.pushMarkdown(subject, description);
    }
}
